package com.vsen.lpr.service;

import com.vsen.lpr.common.model.Response;
import com.vsen.lpr.common.utils.BeanCopyUtil;
import com.vsen.lpr.entity.*;
import com.vsen.lpr.mapper.*;
import com.vsen.lpr.pojo.dto.GetProductDetailDto;
import com.vsen.lpr.pojo.dto.GetProductListDto;
import com.vsen.lpr.pojo.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductService {

    @Autowired
    private ProductInfoMapper productInfoMapper;

    @Autowired
    private ProductTypeMapper productTypeMapper;

    @Autowired
    private ProductLabelMapper productLabelMapper;

    @Autowired
    private LabelTypeMapper labelTypeMapper;

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 商品详情
     *
     * @param dto
     * @return
     */
    public Response<ProductVo> productDetail(GetProductDetailDto dto) {
        ProductInfo productInfo = productInfoMapper.selectByProductId(dto.getProductId());

        return null;
    }

    /**
     * 商品列表
     *
     * @return
     */
//    public Response<List<ProductListVo>> productInfoList() {
//        List<ProductListVo> result = new ArrayList<>();
//        Integer shopId = 1;//写死
//        List<ProductType> productTypes = productTypeMapper.selectByShopId(shopId);
//        for (ProductType p : productTypes) {
//            ProductListVo productListVo = new ProductListVo();
//            List<ProductVo> products = new ArrayList<>();
//            productListVo.setMenuId(p.getId());
//            productListVo.setMenuName(p.getTypeName());
//            List<ProductInfo> productInfos = productInfoMapper.selectByTypeId(p.getId());//商品列表
//            List<ProductVo> productVos = BeanCopyUtil.copyListFromSrcToDest(productInfos, ProductVo.class);
//            for (ProductVo productVo : productVos) {
//                List<ProductLabel> labels = productLabelMapper.selectByProductId(productVo.getId());//商品标签列表
//                List<ProductLabelVo> labelVos = BeanCopyUtil.copyListFromSrcToDest(labels, ProductLabelVo.class);
//                productVo.setLabels(labelVos);
//                products.add(productVo);
//            }
//            productListVo.setProducts(products);
//            result.add(productListVo);
//        }
//        return Response.success(result);
//    }

    /**
     * 商品分类列表
     *
     * @return
     */
    public Response<List<ProductTypeVo>> productTypeList() {
        Integer shopId = 1;
        List<ProductType> list = productTypeMapper.selectByShopId(shopId);
        List<ProductTypeVo> result = BeanCopyUtil.copyListFromSrcToDest(list, ProductTypeVo.class);
        return Response.success(result);
    }

    /**
     * 商品列表（根据商品标签）
     *
     * @param dto
     * @return
     */
    public Response<List<UserProductVo>> productList(GetProductListDto dto) {
        List<ProductInfo> productInfos = productInfoMapper.selectByTypeId(dto.getTypeId());
        List<UserProductVo> productVos = BeanCopyUtil.copyListFromSrcToDest(productInfos, UserProductVo.class);

        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectByUserId(dto.getUserId());
        //统计购物车每个商品的数量
        Map<Integer, Integer> selectCountMap = new HashMap<>();
        for (ShoppingCart sc : shoppingCarts) {
            Integer productId = sc.getProductId();
            if (selectCountMap.containsKey(productId)) {
                selectCountMap.put(productId, selectCountMap.get(productId) + 1);
            } else {
                selectCountMap.put(productId, 1);
            }
        }

        for (UserProductVo userProductVo : productVos) {
            //标签
            List<LabelType> labelTypes = labelTypeMapper.selectByProductId(userProductVo.getId());
            List<ProductLabelVo> labelVos = new ArrayList<>();
            for(LabelType l : labelTypes){
                ProductLabelVo productLabelVo = new ProductLabelVo();
                productLabelVo.setLabelTypeName(l.getLabelTypeName());
                List<ProductLabel> productLabels = productLabelMapper.selectByProductIdAndType(userProductVo.getId(),l.getId());//商品标签列表
                List<LabelVo> labels = BeanCopyUtil.copyListFromSrcToDest(productLabels, LabelVo.class);
                productLabelVo.setLabels(labels);
                labelVos.add(productLabelVo);
            }
            userProductVo.setLabels(labelVos);
            userProductVo.setSelectCount(selectCountMap.get(userProductVo.getId()) == null ? 0 : selectCountMap.get(userProductVo.getId()));
        }
        return Response.success(productVos);
    }
}
